<?php
namespace app\index\service;


use app\index\model\ContractBuModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\EnginTingModel;
use app\index\model\IncomeEnginModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\UserModel;
use think\Db;

class EnginService extends CommonService
{

    public function get_engin_state(){
        return [
            0=>'未开工',
            3=>'在建',
            6=>'竣工',
        ];
    }


    public function get_engin_state_name($enginid, $state){
        $name = '';
        if($state == 0){
            $name = '未开工';
        }
        if($state == 3){
            $name = '在建';
            $tingNew = EnginTingModel::where(['enginid'=>$enginid])->order('id DESC')->find();
            if($tingNew && $tingNew['name'] == '停工'){
                $name = '在建(停工)';
            }
        }
        if($state == 6){
            $name = '竣工';
        }

        return $name;
    }

    public function get_engin_state_name_tag($enginid, $state){
        $name = '';
        if($state == 0){
            $name = '<span class="mindiancheng"></span>未开工';
        }
        if($state == 3){
            $name = '<span class="mindianlan"></span>在建';
            $tingNew = EnginTingModel::where(['enginid'=>$enginid])->order('id DESC')->find();
            if($tingNew && $tingNew['name'] == '停工'){
                $name = '<span class="mindiancheng"></span>在建(停工)';
            }
        }
        if($state == 6){
            $name = '<span class="mindianlv"></span>竣工';
        }

        return $name;
    }

    public function check_engin_name($name, $id=0){
        return !!EnginModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }

    public function get_engin_showinfo($enginid){
        $engininfo = EnginModel::where(['id'=>$enginid])->find();
        $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
        $engininfo['chanid_name'] = DeptModel::get_name_by_pk($engininfo['chanid']);
        $engininfo['man_xiang_name'] = UserModel::get_realname_by_pk($engininfo['man_xiang']);
        return $engininfo;
    }









    public function get_engin_incomeengin($enginid){
        $fee = IncomeEnginModel::where(['deleted_user'=>0,'mal'=>1,'enginid'=>$enginid])->sum('money');
        return $fee;
    }

    public function get_engin_paymentengin($enginid){
        $fee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_paymentbao($enginid){
        $fee = PaymentBaoModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_paymentneikou($enginid){
        $fee = PaymentNeikouModel::where(['deleted_user'=>0,'enginid'=>$enginid])->sum('money');
        return $fee;
    }

    public function get_engin_paymentneifu($enginid){
        $fee = PaymentNeifuModel::where(['deleted_user'=>0,'isturn'=>1,'enginid'=>$enginid])
            ->where('status','=',1)->sum('money');
        return $fee;
    }

    public function get_engin_balance($enginid){
        $income = $this->get_engin_incomeengin($enginid);
        $zhi = $this->get_engin_paymentengin($enginid);
        $zhi += $this->get_engin_paymentbao($enginid);
        $zhi += $this->get_engin_paymentneikou($enginid);
        $zhi += $this->get_engin_paymentneifu($enginid);
        $balance = $income - $zhi;
        return $balance;
    }




    //施工合同合同金额  -  审核完成
    public function get_contract_shi_money($enginid){
        $money = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->value('money');
        return $money;
    }
    //施工合同补充合同金额  -  审核完成
    public function get_contract_shi_bu_money($enginid){
        $shiid = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->value('id');
        $money = ContractBuModel::where(['shiid'=>$shiid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }
    //施工合同变更后金额  -  审核完成
    public function get_contract_shi_final_money($enginid){
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->find();
        $money = $shiinfo['money'];
        $money += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        return $money;
    }





    public function get_user_engin(){
        $str = "find_in_set(".$this->userinfo['id'].",man_zhuan)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zi)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_cheng)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zhian)";
        $where = Db::raw($str);
        $arr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where($where)
            ->column('id');
        return $arr;
    }




    public function get_engin_buture(){
        return ['新建','扩建','改建','迁建','恢复'];
    }

    public function get_engin_yanjie(){
        return ['通过','未通过'];
    }

}
